package com.szxw.BaiduAi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.szxw.BaiduAi.mapper.EmotionMapper;
import com.szxw.BaiduAi.mapper.impl.EmotionImpl;
import com.szxw.BaiduAi.pojo.Emotions;
import com.szxw.BaiduAi.pojo.Items;
import com.szxw.BaiduAi.service.EmotionService;
import com.szxw.BaiduAi.service.NlpService;

import java.util.*;

/**
 * @Author: 苏执小威
 * @Email:1410511702@qq.com
 * @Desc:
 * @Date: 2022/9/19 17:19
 */
public class EmoServiceIml implements EmotionService {

    private EmotionMapper mapper = new EmotionImpl();

    public Object emotion(String text){
        List<Items> items = mapper.selectEmotion(text);
        Object emotions = null;
        if(items.isEmpty()){
            //调用controller，处理业务
            emotions = NlpService.emotion(text);
            Emotions emotion1 = JSON.parseObject(String.valueOf(emotions), Emotions.class);
            mapper.insertEmotion(emotion1);
            //将结果响应给前端
        }else {
            Emotions emotions1 = new Emotions();
            emotions1.setItems(items);
            emotions = JSON.toJSONString(emotions1);
        }
        return emotions;
    }
}
